package com.talent.jiwen.ui.widgets;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.talent.jiwen.R;
import com.talent.jiwen.util.Constant;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 自定义popupWindow
 */

public class SharePopwindow extends PopupWindow {

    private View mView;
//    private OnShareClickListener onShareClickListener;
    private IWXAPI api;
    private int type;
    private int shareType;
    private String invisitUrl;
    private String userName;
    private Activity mContext;

    public SharePopwindow(Activity context) {//, OnShareClickListener onShareClickListener
        super(context);
//        this.onShareClickListener=onShareClickListener;
        api = WXAPIFactory.createWXAPI(context, Constant.APP_ID, false);
        initView(context);
    }

    public SharePopwindow(Activity context, int type,String invisitUrl,String userName,int shareType ) {//, OnShareClickListener onShareClickListener
        super(context);
//        this.onShareClickListener=onShareClickListener;
        api = WXAPIFactory.createWXAPI(context, Constant.APP_ID, false);
        mContext=context;
        this.type=type;
        this.shareType=shareType;
        this.invisitUrl=invisitUrl;
        this.userName=userName;
        initView(context);
    }

    public SharePopwindow(Activity context,int type) {//, OnShareClickListener onShareClickListener
        super(context);
//        this.onShareClickListener=onShareClickListener;
        api = WXAPIFactory.createWXAPI(context, Constant.APP_ID, false);
        this.type=type;
        initView(context);
    }

    private void initView(final Activity context) {
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = mInflater.inflate(R.layout.popupwindow_share, null);
        LinearLayout weiXFriend = (LinearLayout) mView.findViewById(R.id.weixinghaoyou);
        LinearLayout friendster = (LinearLayout) mView.findViewById(R.id.pengyouquan);
        TextView canaleTv = (TextView) mView.findViewById(R.id.share_cancle);


        canaleTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //销毁弹出框
                dismiss();
            }
        });
        //设置按钮监听
        weiXFriend.setOnClickListener(new View.OnClickListener() {//微信朋友
            @Override
            public void onClick(View v) {
                dismiss();
                share(false);
            }
        });
        friendster.setOnClickListener(new View.OnClickListener() {//朋友圈
            @Override
            public void onClick(View v) {
                dismiss();
                share(true);
            }
        });
        this.setContentView(mView);
        this.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        ColorDrawable dw = new ColorDrawable(0x00000000);
        this.setBackgroundDrawable(dw);
        this.setFocusable(true);
        this.setTouchable(true);
        setOutsideTouchable(true);
        if(type==0){
            backgroundAlpha(context, 0.7f);//0.0-1.0
            this.setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss() {
                    backgroundAlpha(context, 1f);
                }
            });
        }
    }

    public void share(boolean friendsCircle){
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = invisitUrl;//分享url
        WXMediaMessage msg = new WXMediaMessage(webpage);
        InputStream is = null;
        Bitmap pic=null;
        try {
            if(shareType==1){
                msg.title = userName+"邀请您加入即问，年入百万不是梦，一起当即问的股东!";
                msg.description = "在线授课，时间自由，工作兼职两不误...";
                is = mContext.getAssets().open("ic_launcher_teacher.png");
            }else if(shareType==2){
                msg.title = userName+"邀请您加入即问，一边学习当学霸，一边赚学费!";
                msg.description = "有问题即可问，在线学习，时间自由...";
                is = mContext.getAssets().open("ic_launcher_student.png");
            }
            pic = BitmapFactory.decodeStream(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        msg.thumbData = bitmap2ByteArray(pic);
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = msg;
        req.scene = friendsCircle ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
        api.sendReq(req);
    }

    private byte[] bitmap2ByteArray(Bitmap thumbBitmap){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        thumbBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }


//    public interface OnShareClickListener{
//
//        void onFriendsterClick();
//
//        void onWeiXFriendClick();
//    }

}
